/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nsip.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Map which initializes a value lazyily if the key is accessed.
 * 
 * @author nico.rehwaldt
 */
public abstract class LazyInitializingMap<T, V> extends HashMap<T, V> {

    public LazyInitializingMap() {}

    public LazyInitializingMap(Map<T, V> m) {
        super(m);
    }
    
    /**
     * Initializes the given value field as a get is performed on the list.
     * 
     * @param key
     * @return
     */
    protected abstract V initialize(Object key);

    @Override
    public V get(Object key) {
        V value = super.get(key);
        if (value == null) {
            value = initialize(key);
            if (value != null) {
                put((T) key, value);
            }
        }

        return value;
    }
}
